//
//  XDPlaceScenicDetailsViewController.m
//  Le way walkers
//
//  Created by YaZhiLiang on 15/5/15.
//  Copyright (c) 2015年 LTXZ. All rights reserved.
//

#import "XDPlaceScenicDetailsViewController.h"

@interface XDPlaceScenicDetailsViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UIImageView *topView;

@property (nonatomic, strong) UIImageView *gifImageView;

@property (nonatomic, assign) CGFloat kImageOriginHight;

@property (nonatomic, retain) UIView *gifView;

@end

@implementation XDPlaceScenicDetailsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    [self createSubViews];
    [self yangTuo];
//    self.automaticallyAdjustsScrollViewInsets = NO;
}

- (void)buttonAction:(UIButton *)button
{
    XDAllScenicViewController *allVC = [[XDAllScenicViewController alloc]init];
    
    allVC.type = self.typeName;
    
    allVC.ScenicID = self.ScenicID;
    
    [self.navigationController pushViewController:allVC animated:YES];
    
}


- (void)getData
{
    
    NSLog(@"12121211212112----%@----11212112----%@-----", self.typeName, self.ScenicID);
    
    [NNetworkHandle getDataWithUrl:[NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/", self.typeName, self.ScenicID] Cookie:@"bt_devid=i_70977b3f76167894392f3ada99cb3f82fb57705ae698d0fc08ca99fd7f26cdf9057e84df; Hm_lvt_39ccc5353de4e1969b414e80e516d5a7=1431606395,1431674200,1431763985,1431910698; sessionid=75f121c0f7165c1e3eef07a24359f126" completion:^(id result) {
        
        self.imageUrl = [[[result objectForKey:@"hottest_places"] objectAtIndex:0] objectForKey:@"photo"];
    
        self.name1 = [result objectForKey:@"name"];
        
        [self.topView sd_setImageWithURL:[NSURL URLWithString:self.imageUrl] placeholderImage:[UIImage imageNamed:@"69E58PIChJC_1024.jpg"]];
        
        self.topName.text = self.name1;
        
        self.title = self.name1;
        
        NSLog(@"213123131%@", self.name1);
        
        self.hottest_sitesArr = [NSMutableArray array];
        self.hottest_sitesArr = [result objectForKey:@"hottest_sites"];
        self.visited_count = [[result objectForKey:@"visited_count"] integerValue];
        self.wish_to_go_count = [[result objectForKey:@"wish_to_go_count"] integerValue];
        
        NSString *str = [NSString stringWithFormat:@"%ld 去过 / %ld 喜欢", self.visited_count, self.wish_to_go_count];
        
        self.likeText.text = str;
        
        NSLog(@"woshiyizhangtupian%@", self.imageUrl);
        
        self.Arr = [NSMutableArray array];
        
        for (NSDictionary *dic in self.hottest_sitesArr) {
            
        XDPlaceSenic *xdPlaceSenic = [[XDPlaceSenic alloc]initWithDic:dic];
            
            [self.Arr addObject:xdPlaceSenic];
            
            [xdPlaceSenic release];
            
        }
        
        [self.collectionView reloadData];
        
       
        if (self.Arr.count == 6) {
            
            self.myButton = [UIButton buttonWithType:UIButtonTypeCustom];
            
            self.myButton.frame = CGRectMake(20, self.kImageOriginHight * 3 + 90, self.view.frame.size.width - 50, 40);
            
            NSLog(@"youduoshaoge%ld", self.Arr.count);
            
            [self.myButton setTitle:@"全部热门景点" forState:UIControlStateNormal];
            
            self.myButton.titleLabel.textAlignment = NSTextAlignmentCenter;
            
            [self.myButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
            
            self.myButton.layer.cornerRadius = 8;
            
            self.myButton.layer.borderWidth = 0.5;
            
            self.myButton.layer.borderColor = [[UIColor lightGrayColor] CGColor];
            
            [self.myButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
            
            [self.collectionView addSubview:self.myButton];
        }
        
        [self.collectionView reloadData];
        
        [self.gifImageView stopAnimating];
        
        self.gifView.hidden = YES;
        
    }];
}

- (void)yangTuo
{
    [self getData];
    self.gifImageView.hidden = NO;
    self.gifImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 40, self.view.frame.size.height / 2 - 50, 80, 100)];
    NSArray *gifArray = [NSArray arrayWithObjects:[UIImage imageNamed:@"cao1.png"],
                         [UIImage imageNamed:@"cao2.png"],
                         [UIImage imageNamed:@"cao3.png"],
                         [UIImage imageNamed:@"cao4.png"],
                         [UIImage imageNamed:@"cao5.png"],
                         [UIImage imageNamed:@"cao6.png"],
                         [UIImage imageNamed:@"cao7.png"],
                         [UIImage imageNamed:@"cao8.png"],
                         [UIImage imageNamed:@"cao9.png"],
                         [UIImage imageNamed:@"cao10.png"],nil];
    self.gifImageView.animationImages = gifArray; //动画图片数组
    self.gifImageView.animationDuration = 0.8; //执行一次完整动画所需的时长
    self.gifImageView.animationRepeatCount = 10000;  //动画重复次数
    self.gifView = [[UIView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
    UIColor *imageB = [UIColor colorWithPatternImage:[UIImage imageNamed:@"123456_123456"]];
    self.gifView.backgroundColor = imageB;
    [self.gifView addSubview:self.gifImageView];
    [self.gifImageView startAnimating];
    [self.view addSubview:self.gifView];
    [_gifImageView release];
    [_gifView release];
}


static CGFloat kTempHeight = 60.0f;

- (void)createSubViews
{
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"iconfont-xiangzuo"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(back:)];
    
    UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc]init];
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowlayout];
    
//    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    UIColor *image1 = [UIColor colorWithPatternImage:[UIImage imageNamed:@"123456_123456"]];
    
    self.collectionView.backgroundColor = image1;
    
    flowlayout.itemSize = CGSizeMake((self.view.frame.size.width - 60) / 2, (self.view.frame.size.width - 60) / 2);
    
    self.kImageOriginHight =(self.view.frame.size.width - 60) / 2;
    
    flowlayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    
    flowlayout.sectionInset = UIEdgeInsetsMake(50, 20, 0, 20);
    
    self.collectionView.delegate = self;
    
    self.collectionView.dataSource = self;
    
    self.collectionView.alwaysBounceVertical = YES;
    
    [self.view addSubview:self.collectionView];
    
    [_collectionView release];
    
    [flowlayout release];
    
    [self.collectionView registerClass:[XDPlaceScenicDetailsCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    self.collectionView.contentInset = UIEdgeInsetsMake(self.kImageOriginHight, 0, 113 + 64, 0);
    
    self.topView = [[UIImageView alloc]init];
    
    self.topView.frame = CGRectMake(0, -self.kImageOriginHight - kTempHeight, self.collectionView.frame.size.width, self.kImageOriginHight + kTempHeight);
    
    self.topView.userInteractionEnabled = YES;
    
    [self.collectionView addSubview:self.topView];
    
    [_topView release];
    
//    UIButton *mybutton = [UIButton buttonWithType:UIButtonTypeCustom];
//    
//    [mybutton setImage:[[UIImage imageNamed:@"iconfont-xiangzuo"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
//    
//    mybutton.frame = CGRectMake(self.collectionView.frame.origin.x + 10, 64 + 20, 50, 30);
//    
//    [mybutton addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
//    
////    mybutton.backgroundColor = [UIColor blackColor];
//    
//    [self.topView addSubview:mybutton];
    
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    
    [self.topView addGestureRecognizer:tap];
    
    [tap release];
    
    self.likeText = [[[UILabel alloc] initWithFrame:CGRectMake(self.topView.frame.origin.x + 5, self.topView.frame.origin.y + self.topView.frame.size.height - 50, 200, 30)] autorelease];
//
//        self.likeText.backgroundColor = [UIColor brownColor];
//
    self.likeText.textColor = [UIColor whiteColor];
    
    [self.collectionView addSubview:self.likeText];
    
    self.topName = [[[UILabel alloc]initWithFrame:CGRectMake(self.likeText.frame.origin.x, self.likeText.frame.origin.y - self.likeText.frame.size.height - 20, self.likeText.frame.size.width, self.likeText.frame.size.height + 10)] autorelease];
    
    self.topName.textColor = [UIColor whiteColor];
    
    self.topName.font = [UIFont fontWithName:@"Arial-BoldItalicMT" size:24];
    
//    self.topName.backgroundColor = [UIColor redColor];
    
    [self.collectionView addSubview:self.topName];
    
    UILabel *mylabel = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 50, self.likeText.frame.origin.y + self.likeText.frame.size.height + 20, 100, 40)];
    
    mylabel.text = @"推荐地点";
    
    mylabel.textAlignment = NSTextAlignmentCenter;
    
    mylabel.font = [UIFont fontWithName:@"Arial-BoldItalicMT" size:24];
    
    [self.collectionView addSubview:mylabel];
    
    [mylabel release];
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.collectionView.delegate = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    self.collectionView.delegate = self;
}

- (void)back:(UIBarButtonItem *)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)tapAction:(UITapGestureRecognizer *)tap
{
    XDPlaceDetailsRegionViewController *xdPlaceDetailsVC = [[[XDPlaceDetailsRegionViewController alloc]init] autorelease];
    
    xdPlaceDetailsVC.ScenicID = self.ScenicID;
    
    xdPlaceDetailsVC.typeName = self.typeName;
    
    xdPlaceDetailsVC.titleName = self.name1;
    
    NSLog(@"---------- ****** %@", xdPlaceDetailsVC.ScenicID);
    
    [self.navigationController pushViewController:xdPlaceDetailsVC animated:YES];
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//    static NSInteger i = 0;
//    if ((i = (scrollView.contentOffset.y + 60) / 3)) {
//        self.navigationController.navigationBar.alpha = 0.1 * i ;
//    }
    
    CGFloat yOffset  = scrollView.contentOffset.y;
    NSLog(@"yOffset===%f",yOffset);
    CGFloat xOffset = (yOffset + self.kImageOriginHight)/2;
    if (yOffset < -self.kImageOriginHight) {
        CGRect f = self.topView.frame;
        f.origin.y = yOffset - kTempHeight;
        f.size.height =  -yOffset + kTempHeight;
        f.origin.x = xOffset;
        f.size.width = self.view.frame.size.width + fabsf(xOffset)*2;
        self.topView.frame = f;
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSLog(@"----------------%ld", self.Arr.count);
    
    return self.Arr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    XDPlaceScenicDetailsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    XDPlaceSenic *senic = self.Arr[indexPath.row];
    
    cell.xdSenic = senic;
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
 
    XDPlaceRegionViewController *regionVC = [[XDPlaceRegionViewController alloc]init];
    
    XDPlaceScenicDetailsCollectionViewCell *cell = (XDPlaceScenicDetailsCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    
    XDPlaceCityViewController *cityVC = [[[XDPlaceCityViewController alloc]init] autorelease];
    
    regionVC.typeName = cell.typeName;
    
    regionVC.ScenicID = cell.ScenicID;
    
    cityVC.typeName = cell.typeName;
    
    cityVC.ScenicID = cell.ScenicID;
    
    
    NSLog(@"----------### %@", cell.typeName);
    
    if ([regionVC.typeName integerValue] == 1 || [regionVC.typeName integerValue] == 2 || [regionVC.typeName integerValue] == 3) {
        
        
        [self.navigationController pushViewController:regionVC animated:YES];
        
    }
    
    if ([regionVC.typeName integerValue] == 5) {
        
        [self.navigationController pushViewController:cityVC animated:YES];
    }
    
//            [self.navigationController pushViewController:regionVC animated:YES];
    
}

//- (UILabel *)scrollViewItemWithY:(CGFloat)y andNumber:(int)num {
////[UIScreen mainScreen].bounds.size.width-20
//    UILabel *item = [[UILabel alloc] initWithFrame:CGRectMake(10, y,150, 180)];
//    
//    item.userInteractionEnabled = YES;
//    
//    [item setBackgroundColor:[self randomColor]];
//    [item setText:[NSString stringWithFormat:@"Item %d", num]];
//    [item setTextAlignment:NSTextAlignmentCenter];
//    [item setFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:26]];
//    [item setTextColor:[UIColor whiteColor]];
//    return item;
//}

//- (UIColor *)randomColor {
//    CGFloat hue = ( arc4random() % 256 / 256.0 );  //  0.0 to 1.0
//    CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from white
//    CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from black
//    return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
